package controllers;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.ButtonRequest;
import model.PendingTaarifModel;
import view.PendingTaarifGUI;
import client.CliMessage;

public class PendingTaarifController  extends AbstractController
{	
	private PendingTaarifGUI PendingTaarifView;
	private PendingTaarifModel PendingTaarifModel;
	private boolean SearchFlag = false;


	
	/**
	 * This is pending tarif controller constructor.
	 * @param PendingtaarifView is a pending taarif window GUI instance.
	 * @param PendingTaarifModel is a ButtonRequest Model instance.
	 */
	public PendingTaarifController(PendingTaarifGUI PendingTaarifView, PendingTaarifModel PendingTaarifModel) 
	{
		super(PendingTaarifView,PendingTaarifModel);
		this.PendingTaarifView = PendingTaarifView;
		this.PendingTaarifModel = PendingTaarifModel;
	
	}
	
	/**
	 * This is ApprovePendingTaarifButtons method-this method add listeners to buttons in Taarif window.
	 * 
	 */
	
	private void setApproveButtons(ArrayList<ButtonRequest>  btnReq)
	{
		
		int nButtons = btnReq.size();
				
		//set a HashMap that contains all possible button listeners by their names
		ArrayList<ApproveButtonListener> al = new ArrayList<ApproveButtonListener>();
		ArrayList<ApproveButtonListener> btnLis = new ArrayList<ApproveButtonListener>();
		
		//set  specific approve buttons
		for(int i = 0; i < nButtons ; i++)
		{
			al.add(new ApproveButtonListener(btnReq.get(i).getButtonReqID()));
			btnLis.add(al.get(i));
			PendingTaarifView.addAppButtonListener(btnLis.get(i),i);
		}	
	
	}

	/**
	 * This is denyPendingTaarifButtons method-this method add listeners to buttons in Taarif window.
	 * 
	 */
	
	private void setdenyButtons(ArrayList<ButtonRequest>  btnReq)
	{
		
		int nButtons = btnReq.size();
		
		
		//set a HashMap that contains all possible button listeners by their names
		ArrayList<denyButtonListener> al = new ArrayList<denyButtonListener>();
		ArrayList<denyButtonListener>  btnLis=new ArrayList<denyButtonListener>();
		
		//set  specific approve buttons
		for(int i = 0; i < nButtons ; i++)
		{
			al.add(new denyButtonListener(btnReq.get(i).getButtonReqID()));
			btnLis.add(al.get(i));
			PendingTaarifView.addDenButtonListener(btnLis.get(i),i);
		}	
	
	}
	
	
	/**
	 * Inner class that handles when Approve Button Pressed by request Id, implements Action listener
	 * @author Tal & Yuda
	 *
	 */
	
	class ApproveButtonListener extends AbstractListener
	{
	
		private int TID;
	
		
		public ApproveButtonListener ( String TID)
		{
			this.TID = Integer.parseInt(TID);
	
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			
			
			String searchQuery = "search"	+ " SELECT tar.newprice,tar.serviceid"
					                        + " FROM taarif_history AS tar "
			                                + " WHERE tar.tid = " + TID + "";
			
            sendToServer(searchQuery);
            
          
            
            String updateQuary1 = "adu" + "UPDATE taarif_history "
                                        + " SET appbyceo = 1"
		                                + " WHERE tid = " + TID + "";
			 
           
           		
            sendToServer(updateQuary1);
			
			//remove buttons from arrayList
			PendingTaarifModel.RemoveButtonRequestApp();
			PendingTaarifModel.RemoveButtonRequestDen();
			
			String searchQueryFINAL = "search"	+ "SELECT * "
                                                + " FROM taarif_history "
                                                + " WHERE appbyceo = 0 ";
			
			                 
             sendToServer(searchQueryFINAL);						                       											
		}
	}
	
	
	/**
	 * Inner class that handles when deny Button Pressed by request Id, implements Action listener
	 * @author Tal & Yuda
	 *
	 */

	class denyButtonListener extends AbstractListener
	{
		
		private int TID;
	
		public denyButtonListener ( String TID)
		{
			this.TID = Integer.parseInt(TID);
		}
	
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{		
		
			
            String updateQuary = "adu" + "UPDATE taarif_history "
                                       + " SET appbyceo = 2"
		                               + " WHERE tid = " + TID + "";
            
          //remove buttons from arrayList
			PendingTaarifModel.RemoveButtonRequestApp();
			PendingTaarifModel.RemoveButtonRequestDen();
		
            
			String searchQuery = "search"	+"SELECT * "
				             	+" FROM taarif_history "
					            +" WHERE appbyceo = 0 ";
			
			
			sendToServer(updateQuary);	
			sendToServer(searchQuery);						                       											
		}	
	}

	
	

	@Override
	public void handleDBRequestSuccess()
	{
		PendingTaarifView.displayWrnMessage("Pending Request Updated.");
	}
	
	@Override
	public void handleDBSearchResult(CliMessage dbResult) 
	{	
		
			//get a list of all taarif request
			ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
			ArrayList<ButtonRequest>  btnReqApp = PendingTaarifModel.getButtonRequestApp();
			ArrayList<ButtonRequest>  btnReqDen = PendingTaarifModel.getButtonRequestDen();
			int rows=srs.size();
			int column=6;
		
			//create data table
			Object[][] data=new Object[rows][column];
	   	
			String[] columnNames={"Tid","Service ID", "Description", "Old Price", "New Price", "Date"};

		
			for(int i = 0 ; i <rows  ; i++)
			{
				Map<String, String> taarifsList  = srs.get(i);	
				
				if(	taarifsList.get("OLDPRICE")!=null)
				{
					//add TID button to arraylist
					btnReqApp.add(new ButtonRequest(taarifsList.get("TID"),(taarifsList.get("TID"))));
					btnReqDen.add(new ButtonRequest(taarifsList.get("TID"),(taarifsList.get("TID"))));
					
					// tid
					data[i][0]=taarifsList.get("TID");
					// service id
					data[i][1]=taarifsList.get("SERVICEID");
					// description
					data[i][2]=taarifsList.get("DESCRIPTION");
					// old price
					data[i][3]=taarifsList.get("OLDPRICE");
					// new price
					data[i][4]=taarifsList.get("NEWPRICE");
					// date
					data[i][5]=taarifsList.get("DATE");
			
					System.out.println("tid: "+taarifsList.get("TID")+" "+ "Item number: "+taarifsList.get("SERVICEID")+" "+"description: "+taarifsList.get("DESCRIPTION")+" "
					 									+"old price: "+taarifsList.get("OLDPRICE")+ " "+"new price: "+taarifsList.get("NEWPRICE") + " " +"date: "+taarifsList.get("DATE"));
				}	 
				else
				{
				

					System.out.println("TID: "+ taarifsList.get("TID") + " "+ "service ID: "+taarifsList.get("SERVICEID")+" "+"price: "+ taarifsList.get("NEWPRICE"));
					
					//initialzie taarif		
					int serviceID=Integer.parseInt(taarifsList.get("SERVICEID"));
					double price= Double.parseDouble(taarifsList.get("NEWPRICE"));
					
					String updateQuary2 = "adu" + "UPDATE taarif " 
                        	+ " SET price = " + price + ""
                            + " WHERE serviceid = " + serviceID + "";
 
                        
                              sendToServer(updateQuary2);

				
				}
			}	
		
			//set new data table
			PendingTaarifView.setTable(columnNames, data);
		
			//create n buttons
			int numberOfRequests = btnReqApp.size();
			
		
			PendingTaarifView.setApproveTaarifbuttons(numberOfRequests);
			PendingTaarifView.setdenyTaarifbuttons(numberOfRequests);
		
			//Set list button  listener	
			setApproveButtons(btnReqApp);
			setdenyButtons(btnReqDen); 
		
			//display main window
			PendingTaarifView.setVisible(true);
			PendingTaarifView.setTableVisibilty(true);	
			
		
				
	}

	@Override
	public void handleNullSearchResult() 
	{
		//display main window
		PendingTaarifView.setVisible(true);
		PendingTaarifView.disposeTable();
		PendingTaarifView.disposeAppExistingButtons();
		PendingTaarifView.disposeDenExistingButtons();
		PendingTaarifView.displayWrnMessage("Pending Taarif not Found");
		
	}
}
